package com.ssfk.service.impl;

import java.util.HashMap;
import java.util.Map;

class TableField {
    // 名字
    private String name;
    // 类型
    private String type;
    // 值
    private String value;
    // 可替换的
    boolean replaced;
    // 必选的
    boolean required;
    // 占位对象
    private Map<String, Placeholder> placeholders;

    public TableField() {
        placeholders = new HashMap<>();
    }

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    public String getName() {
        return name;
    }

    public void setName(String fieldName) {
        this.name = fieldName;
    }

    public String getType() {
        return type;
    }

    public void setType(String jdbcType) {
        this.type = jdbcType;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String fieldValue) {
        this.value = fieldValue;
    }

    public boolean isReplaced() {
        return replaced;
    }

    public void setReplaced(boolean replaced) {
        this.replaced = replaced;
    }

    public Map<String, Placeholder> getPlaceholders() {
        return placeholders;
    }

    public void setPlaceholders(Map<String, Placeholder> placeholders) {
        this.placeholders = placeholders;
    }

    public void createValue(String valuesExpr) {
        Map<String, Placeholder> valuePlaceholders = Placeholder
                .createPlaceholders(valuesExpr, false);

        TableField field = this;
        // set placeholders
        // 字段值中有变量,在build中进行替换
        if (valuePlaceholders.size() > 0) {
            field.setValue(valuesExpr);
            field.setReplaced(true);
            boolean required = false;
            // 如果有一个变量是必须的，则这个字段是必须的
            for (String name : valuePlaceholders.keySet()) {
                required |= valuePlaceholders.get(name).isRequired();
            }
            field.setRequired(required);
            // 将变量添加列表中
            this.placeholders.putAll(valuePlaceholders);
        } else {
            field.setValue(valuesExpr);
            field.setReplaced(false);
        }
    }

}
